/* eslint-disable no-prototype-builtins */
export const setDocumentTitle = function (title) {
  document.title = title
  const ua = navigator.userAgent
  // eslint-disable-next-line
  const regex = /\bMicroMessenger\/([\d\.]+)/
  if (regex.test(ua) && /ip(hone|od|ad)/i.test(ua)) {
    const i = document.createElement('iframe')
    // i.src = '/favicon.ico'
    i.style.display = 'none'
    i.onload = function () {
      setTimeout(function () {
        i.remove()
      }, 9)
    }
    document.body.appendChild(i)
  }
}

export const domTitle = 'BFDA-PMS'

//得到dom的offset属性，包含height width top right bottom left x y
export function getDomOffset(dom) {
  return dom.getBoundingClientRect()
}

//获取当前视图的大小
export function getViewPortOffset() {
  if (window.innerWidth) {
    return {
      w: window.innerWidth,
      h: window.innerHeight
    }
  } else {
    // ie8及其以下
    if (document.compatMode === 'BackCompat') {
      // 怪异模式
      return {
        w: document.body.clientWidth,
        h: document.body.clientHeight
      }
    } else {
      // 标准模式
      return {
        w: document.documentElement.clientWidth,
        h: document.documentElement.clientHeight
      }
    }
  }
}

//得到计算属性
export function getStyleComputedProperty(element, property) {
  // NOTE: 1 DOM access here
  let css = document.defaultView.getComputedStyle(element, null)
  return css[property]
}

/**
 * Returns the scrolling parent of the given element
 * @function
 * @ignore
 * @argument {Element} element
 * @returns {Element} offset parent
 */
export function getScrollParent(element) {
  let parent = element.parentNode
  if (!parent) {
    return element
  }
  if (parent === window.document) {
    if (window.document.body.scrollTop || window.document.body.scrollLeft) {
      return window.document.body
    } else {
      return window.document.documentElement
    }
  }
  if (
    ['scroll', 'auto'].indexOf(getStyleComputedProperty(parent, 'overflow')) !== -1 ||
    ['scroll', 'auto'].indexOf(getStyleComputedProperty(parent, 'overflow-x')) !== -1 ||
    ['scroll', 'auto'].indexOf(getStyleComputedProperty(parent, 'overflow-y')) !== -1
  ) {
    return parent
  }
  return getScrollParent(element.parentNode)
}

export function hasClass(obj, cls) {
  let reg = new RegExp('(\\s|^)' + cls + '(\\s|$)')
  return obj.className.match(reg)
}
//添加class的方法
export function addClass(obj, cls) {
  let Obj = obj
  let objClass = obj.className
  let blank = objClass !== '' ? ' ' : ''
  if (!hasClass(obj, cls)) {
    let added = objClass + blank + cls
    Obj.className = added
  }
}
//删除class的方法
export function removeClass(obj, cls) {
  let Obj = obj
  let reg = new RegExp('(\\s|^)' + cls + '(\\s|$)')
  if (hasClass(obj, cls)) {
    Obj.className = obj.className.replace(reg, ' ')
  }
}

// eslint-disable-next-line no-useless-escape
const SPECIAL_CHARS_REGEXP = /([\:\-\_]+(.))/g
const MOZ_HACK_REGEXP = /^moz([A-Z])/
/* istanbul ignore next */
const camelCase = function (name) {
  return name
    .replace(SPECIAL_CHARS_REGEXP, function (_, separator, letter, offset) {
      return offset ? letter.toUpperCase() : letter
    })
    .replace(MOZ_HACK_REGEXP, 'Moz$1')
}

/*得到dom当前样式的值*/
export function getStyle(element, styleName) {
  if (!element || !styleName) return null
  styleName = camelCase(styleName)
  if (styleName === 'float') {
    styleName = 'cssFloat'
  }
  try {
    let computed = document.defaultView.getComputedStyle(element, '')
    return element.style[styleName] || computed ? computed[styleName] : null
  } catch (e) {
    return element.style[styleName]
  }
}

/* 给dom设置style样式 */
export function setStyle(element, styleName, value) {
  if (!element || !styleName) return

  if (typeof styleName === 'object') {
    for (let prop in styleName) {
      if (styleName.hasOwnProperty(prop)) {
        setStyle(element, prop, styleName[prop])
      }
    }
  } else {
    styleName = camelCase(styleName)
    if (styleName === 'opacity') {
      element.style.filter = isNaN(value) ? '' : 'alpha(opacity=' + value * 100 + ')'
    } else {
      element.style[styleName] = value
    }
  }
}

export function getMaxIdx(selector) {
  const showPortals = document.querySelectorAll(selector)
  if (showPortals && showPortals.length > 0) {
    const zIndexs = []
    showPortals.forEach((it) => {
      return zIndexs.push(getStyle(it, 'zIndex'))
    })
    return Math.max(...zIndexs)
  } else {
    return 0
  }
}
